package com.xunshangwang.pinhaohuo.ui.activities.address;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.entity.Address;
import com.xunshangwang.pinhaohuo.ui.activities.BaseActivity;
import com.xunshangwang.pinhaohuo.widgets.LoadLayout;

import tool.library.TextFormTool;
import tool.library.ToastTool;

/**
 * Created by xzp on 16/2/15.
 */
public abstract class BaseAddressActivity extends BaseActivity implements View.OnClickListener{
    Address mAddress;
    EditText mLongAddressEdit,mPhoneEdit,mReceiveNameEdit;
    TextView mAddressText;
    LoadLayout mLoad;
    View mSelectAddressBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_address);
        mSelectAddressBtn=this.findViewById(R.id.select_address_btn);
        mSelectAddressBtn.setOnClickListener(this);
        this.findViewById(R.id.save_btn).setOnClickListener(this);
        mAddressText= (TextView) this.findViewById(R.id.address_name_text);
        mLongAddressEdit= (EditText) this.findViewById(R.id.long_address_edit);
        mPhoneEdit= (EditText) this.findViewById(R.id.mobile_phone_edit);
        mReceiveNameEdit= (EditText) this.findViewById(R.id.receive_name_edit);
        mLoad= (LoadLayout) this.findViewById(R.id.load_layout);
        if(savedInstanceState==null)
            initAddress();
        else {
            mAddress= (Address) savedInstanceState.getSerializable("address");
            mAddressText.setText(savedInstanceState.getString("addressText"));
            mLongAddressEdit.setText(savedInstanceState.getString("longAddress"));
            mPhoneEdit.setText(savedInstanceState.getString("phone"));
            mReceiveNameEdit.setText(savedInstanceState.getString("receiveName"));
        }

    }
    abstract void initAddress();
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("address", mAddress);
        outState.putString("addressText", mAddressText.getText().toString());
        outState.putString("longAddress",mLongAddressEdit.getText().toString());
        outState.putString("phone",mPhoneEdit.getText().toString());
        outState.putString("receiveName",mReceiveNameEdit.getText().toString());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.select_address_btn:
                startActivityForResult(new Intent(this,SelectAddressActivity.class),0);
                break;
            case R.id.save_btn:
                if(mAddressText.getText().toString().length()==0) {
                    ToastTool.showToast("请选择所在地区");
                    return;
                }
                if(mLongAddressEdit.getText().toString().trim().length()==0){
                    ToastTool.showToast("请输入详细地址");
                    return;
                }
                if(mReceiveNameEdit.getText().toString().trim().length()==0){
                    ToastTool.showToast("请输入收货人姓名");
                    return;
                }
                if(mPhoneEdit.getText().toString().trim().length()==0){
                    ToastTool.showToast("请输入收货人联系电话");
                    return;
                }
                if(!TextFormTool.checkPhoneForm(mPhoneEdit.getText().toString().trim())){
                    ToastTool.showToast("收货人联系电话不符合规范");
                    return;
                }
                View focus=getCurrentFocus();
                if(focus!=null){
                    InputMethodManager inputMethodManager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(focus.getWindowToken(),0);
                }
                requestSave();
                break;
        }
    }
    abstract void requestSave();
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode!=RESULT_OK)
            return;
        switch (requestCode){
            case 0:
                mAddress.setCity_id(data.getIntExtra("cityId",0));
                mAddressText.setText(data.getStringExtra("address"));
                mAddress.setProvince_id(data.getIntExtra("provinceId", 0));
                mAddress.setDistrict_id(data.getIntExtra("districtId",0));
                break;
        }
    }
}
